﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

namespace Edutainment
{
	/// <summary>
	/// Interaktionslogik für Sortieralgorithmen.xaml
	/// </summary>
	public partial class BubbleSortWindow : UserControl
	{
        BubbleSortExerciseWindow bubbleSortExercise;
        BubbleSortExamWindow bubbleSortExam;
        Grid bubbleSortExplaination;
        String currentState = "Overview";

        bool isPlaying;
        bool mediaLoaded;

		public BubbleSortWindow()
		{
			this.InitializeComponent();
            bubbleSortExercise = new BubbleSortExerciseWindow();
            bubbleSortExam = new BubbleSortExamWindow();
            bubbleSortExplaination = (Grid)VisualTreeHelper.GetChild(this.mainContent, 0);
            isPlaying = false;
            mediaLoaded = false;
		}

        private void deselectOther(ToggleButton btnA, ToggleButton btnB)
        {
            if (btnA != null && (bool)btnA.IsChecked)
                btnA.IsChecked = false;
            else if (btnB != null && (bool)btnB.IsChecked)
                btnB.IsChecked = false;

        }

        private void animteButtonVisibility(bool visibile)
        {
            double to, from;
            if (visibile)
            {
                to = 1.0;
                from = 0.0;
            }
            else
            {
                to = 0.0;
                from = 1.0;
            }

            Duration dur = new Duration(TimeSpan.FromSeconds(0.5));

            DoubleAnimation dAnim = new DoubleAnimation();
            dAnim.Duration = dur;
            dAnim.From = from;
            dAnim.To = to;

            Storyboard.SetTarget(dAnim, btn_play);
            Storyboard.SetTargetProperty(dAnim, new PropertyPath(UIElement.OpacityProperty));

            Storyboard sb = new Storyboard();
            sb.Children.Add(dAnim);
            sb.Begin();

        }

        private void btn_play_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (video.Source != null && mediaLoaded)
            {
                if (!isPlaying)
                {
                    video.Play();
                    isPlaying = true;
                    animteButtonVisibility(false);

                    /*MainWindow mWin = null;

                    mWin = (MainWindow)((App)App.Current).MainWindow;
                    Grid toAdd = mWin.rootGrid;

                    double width = mWin.Width;
                    double height = mWin.Height;

                    Thickness margin = new Thickness(0, 0, 0, 0);
                    video.Margin = margin;
                    video.Height = height;
                    video.Width = width;
                    playPauseButton.Margin = margin;
                    playPauseButton.Height = height;
                    playPauseButton.Width = width;

                    ((Grid)video.Parent).Children.Remove(video);
                    ((Grid)playPauseButton.Parent).Children.Remove(playPauseButton);

                    toAdd.Children.Add(video);
                    toAdd.Children.Add(playPauseButton);*/

                }
                else
                {
                    video.Pause();
                    isPlaying = false;
                    animteButtonVisibility(true);
                }
            }
        }

        private void btn_pause_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            video.Pause();
        }

        private void Image_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {

        }

        private void btn_exercises_MouseDown(object sender, MouseButtonEventArgs e)
        {
            loadExercise();          
        }

        private void loadExercise()
        {
            if (currentState.CompareTo("Exercise") != 0)
            {
                mainContent.Children.Clear();
                //   btn_exercises.ClickMode = ClickMode.Hover;
                mainContent.Children.Add(bubbleSortExercise);
                bubbleSortExercise.showButtons(true);
            }
            currentState = "Exercise";
        
        }

        private void loadExams()
        {
            if (currentState.CompareTo("Exam") != 0)
            {
                mainContent.Children.Clear();
                mainContent.Children.Add(bubbleSortExam);
                bubbleSortExam.showButtons(true);
            }
            currentState = "Exam";
        }


        private void btn_exercises_TouchDown_1(object sender, TouchEventArgs e)
        {
            loadExercise();
        }

        private void btn_exercises_Click_1(object sender, RoutedEventArgs e)
        {
            btn_exercises.IsChecked = true;
            deselectOther(btn_exams, btn_overview);
            loadExercise();
        }

        private void btn_exams_Click_1(object sender, RoutedEventArgs e)
        {
            btn_exams.IsChecked = true;
            deselectOther(btn_exercises, btn_overview);
            loadExams();
        }


        private void loadOverview()
        {
            if (currentState.CompareTo("Overview") != 0)
            {
                mainContent.Children.Clear();
                mainContent.Children.Add(bubbleSortExplaination);
       
            }
            currentState = "Overview";
        }
        private void btn_Overview(object sender, RoutedEventArgs e)
        {
            btn_overview.IsChecked = true;
            deselectOther(btn_exams, btn_exercises);
            loadOverview();
        }


        private void video_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	if (btn_play.Visibility == Visibility.Visible)
            {
                btn_play.Visibility = Visibility.Hidden;
                video.Play();
            }else if (btn_play.Visibility == Visibility.Hidden) {
                btn_play.Visibility = Visibility.Visible;
                video.Pause();
            }
        }

        private void mediaDidLoad(object sender, RoutedEventArgs e)
        {
            mediaLoaded = true;
            txtBlock_duration.Text = String.Format("Dauer: {0:mm\\:ss} Min", video.NaturalDuration.TimeSpan);
        }
	}
}